<?php
/**
 * 十一选五开奖。 中奖不在这里
 */
defined('IN_YuLin') || exit('NO PERMIT!');
require_once (ROOT_PATH.'module/lottery/script_common.inc.php');
echo '<pre>';
$beginTime = time();

$logFile   = '/www/wwwroot/one.qipai101.com/crontab/log/';
// 获取十一选五所有的彩种信息
$lotterySql  = 'SELECT lott.id, lott.`sname`, lott.`title`, lott.`ptid`, lott.`credit`
                FROM ' . Table('lottery') . ' AS lott
                LEFT JOIN ' . Table('lottery_category') . ' AS cate
                ON lott.`cate_id` = cate.`id`
                WHERE cate.`sname` = "syxw"';
$lotteryList = $db->getall($lotterySql);
$creditPlayTab = Table('lottery_credit_play');
// 获取到所有的十一选五彩种，根据彩种设置sname信息来获取开奖接口内容
$qihaoTab    = Table('lottery_k3');

foreach ($lotteryList as $k => $v){
    $openCode = '';
    $target   = '';
    $getUrl   = '';
    switch ($v['sname']){
        case 'gd11x5':
            $openCode = open_syxw_1('gd11', 20);
            break;
        case 'sh11x5':
            $openCode = open_syxw_1('sh11', 20);
            break;
        case 'sd11x5':
            $openCode = open_kkcz('sd11x5kj', 5, 2, 20, '20');
            break;
        case 'jx11x5':
            $openCode = open_kkcz('jx11x5kj', 5, 2, 20, '20');
            break;
    }
    // 检查开奖设置，如果是信用彩种，按照后台设置的方式计算开奖号
    if ($v['credit']){
        // 获取彩种的开奖设置
        $creditPlaySql  = 'SELECT id, type_id, lott_id, open_param FROM ' . $creditPlayTab . ' WHERE lott_id = ' . $v['id'] . ' AND status = 1';
        $creditPlayInfo = $db->getrow($creditPlaySql);
        switch ($creditPlayInfo['type_id']){
            case '1': // 第一种按照该彩种当期投注的半分比来计算中奖号码
                break;
            case '2': // 第二种按照设置的彩种id的上一期开彩号码*随机数计算
                // 获取指定彩种上一期的开彩号码
                $shangqiSql    = 'SELECT `code` FROM ' . $qihaoTab . ' WHERE tid = ' . trim($creditPlayInfo['open_param']) . ' AND `code` != "" ORDER BY starttime DESC limit 1';
                $shangqiInfo   = $db->getrow($shangqiSql);
                if (!$shangqiInfo['code'] || !$shangqiInfo) break;
                $rd       = rand(1, 9);
                $lastCode = explode(',', $shangqiInfo['code']);
                $tmpCode  = array();
                foreach ($lastCode as $lk => $lv){
                    $lv   = substr(intval($lv * $rd), -2);
                    while ($lv > 11){
                        $lv = $lv - 11;
                    }
                    if ($lv < 10){
                        $lv = '0' . intval($lv);
                    }
                    array_push($tmpCode, $lv);
                }
                $lottingSql          = 'SELECT id, tid, period, starttime, stoptime, day, opentime, `code` FROM ' . $qihaoTab . ' WHERE tid = ' . $v['id'] . ' AND stoptime < ' . time() . ' ORDER BY starttime DESC limit 1';
                $lottingInfo         = $db->getrow($lottingSql);
                if ($lottingInfo['code']) break;
                $tmpArr              = array(
                    'expect'         => $lottingInfo['period'],
                    'opencode'       => implode(',', $tmpCode)
                );
                break;
            default:
                break;
        }
        $openCode[] = $tmpArr;
    }
    echo $v['sname'];
    print_r($openCode);
    if (!$openCode) continue;
    // 检查当前获取的开奖记录， 在本系统中有没有生成开奖成功
    foreach ($openCode as $key => $value){
        // 检查当前彩种期号是否有开奖
        $checkFlagSql = 'SELECT id, `code` FROM ' . Table('lottery_k3') . ' WHERE tid = "' . $v['id'] . '" AND period = "' . $value['expect'] . '" AND stoptime < ' . $beginTime . ' ORDER BY starttime DESC limit 1';
        $checkInfo    = $db->getrow($checkFlagSql);
        // 有开奖记录，则跳过
        if($checkInfo['code'] != '' || !$checkInfo['id']) continue;
        // 更新当前彩种期号的开奖结果
        $flag = $db->update('lottery_k3', array('code' => $value['opencode'], 'opentime' => time(), 'status' => '1'), 'id = ' . $checkInfo['id']);
        var_dump($flag);
        $lotStr = date('Y-m-d H:i:s', time()) . ': 开奖 ' . $v['title'] . ' 第' . $value['expect'] . '期，开奖号（' . $value['opencode'] . '）';
        openCodeLog($logFile, 'syxw_open_log_' . date('ymd') . '.txt', $lotStr);
    }
}

echo 'end';